n = int(input())
l = list(map(int, input().split()))

def permute(nums):
    res = []
    def perm(n, p, q):
        if p == q:
            res.append(n[:])
        else:
            for i in range(p, q + 1):
                n[i], n[p] = n[p], n[i]
                perm(n, p + 1, q)
                n[i], n[p] = n[p], n[i]

    perm(nums, 0, len(nums) - 1)
    return res

ss = permute(l)
res = 0
for x in ss:
    summ = 0
    i=1
    while i < n:
        summ += abs(x[i]-x[i-1])
        i += 1
    res += summ
print(res % 1000000007)
